﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;

namespace XamlCleaner.Core.Writer
{
    /// <summary>
    /// Represent an XamlWriter.
    /// </summary>
    public class XamlWriter : IXamlWriter, IDisposable
    {
        #region Members
        /// <summary>
        /// XmlDocument to write
        /// </summary>
        XmlDocument xmlDoc;

        /// <summary>
        /// Writer object helper
        /// </summary>
        XmlWriter xmlWriter;

        #endregion

        #region Constructors

        /// <summary>
        /// Initializes a new instance of XamlCleaner.Core.Writer.XamlWriter class 
        /// with the XmlDocument and the file path where to write.
        /// </summary>
        /// <param name="fileNamePath">Path of file to write <paramref name="document"/></param>
        /// <param name="document">XmlDocument to write</param>
        public XamlWriter(string fileNamePath, XmlDocument document)
        {
            xmlDoc = document;
            this.xmlWriter = XmlWriter.Create(fileNamePath, XmlWriterSettings());
        }

        /// <summary>
        /// Initializes a new instance of XamlCleaner.Core.Writer.XamlWriter class 
        /// with the XmlDocument and the XmlWriter with settings.
        /// </summary>
        /// <param name="outputWriter">XmlWriter who write <paramref name="document"/></param>
        /// <param name="document">XmlDocument to write</param>
        public XamlWriter(XmlWriter outputWriter, XmlDocument document)
        {
            xmlDoc = document;
            this.xmlWriter = outputWriter;
        }

        /// <summary>
        /// Initializes a new instance of XamlCleaner.Core.Writer.XamlWriter class 
        /// with the XmlDocument and the output Stream.
        /// </summary>
        /// <param name="outputStream">Output stream to write <paramref name="document"/></param>
        /// <param name="document">XmlDocument to write</param>
        public XamlWriter(Stream outputStream, XmlDocument document)
        {
            xmlDoc = document;
            this.xmlWriter = XmlWriter.Create(outputStream);
        }

        /// <summary>
        /// Initializes a new instance of XamlCleaner.Core.Writer.XamlWriter class 
        /// with the XmlDocument and the output Stream.
        /// </summary>
        /// <param name="outputTextWriter">Output TextWriter to write <paramref name="document"/></param>
        /// <param name="document">XmlDocument to write</param>
        public XamlWriter(TextWriter outputTextWriter, XmlDocument document)
        {
            xmlDoc = document;
            this.xmlWriter = XmlWriter.Create(outputTextWriter);
        }

        #endregion

        #region Private Methods

        private XmlWriterSettings XmlWriterSettings()
        {
            XmlWriterSettings settings = new XmlWriterSettings();

            settings.Indent = true;
            settings.NewLineOnAttributes = true;
            settings.OmitXmlDeclaration = true;
            settings.CloseOutput = true;

            return settings;
        }

        #endregion

        #region IXamlWriter Methods

        public void Write()
        {
            this.xmlDoc.WriteTo(xmlWriter);
            this.xmlWriter.Close();
        }

        #endregion

        #region IDisposable Methods

        public void Dispose()
        {
            if (this.xmlWriter.WriteState != WriteState.Closed)
                this.xmlWriter.Close();
            this.xmlWriter = null;
            this.xmlDoc = null;
        }

        #endregion
    }
}
